package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.csweimei.rentmanagementtreasure.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.adapter.HouseCxAdapter;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.postBean.PostHouseInfoBean;
import com.csweimei.rentmanagementtreasure.pop.PopBean;
import com.csweimei.rentmanagementtreasure.pop.PopCustomize;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.pop.PopWheelBase;
import com.csweimei.rentmanagementtreasure.viewModel.PopSelVM;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class AddMoreVM extends TitleVM {
    public ObservableField<PostHouseInfoBean> entity = new ObservableField<>(new PostHouseInfoBean());

    public AddMoreVM(@NonNull Application application) {
        super(application);
        setTitleText("更多信息");
    }

    public ObservableField<String> floor = new ObservableField<>();
    public ObservableField<String> lift = new ObservableField<>();
    public ObservableField<String> renovation = new ObservableField<>();
    public ObservableField<String> toward = new ObservableField<>();
    public ObservableField<String> facilities = new ObservableField<>();
    public ObservableField<String> RoomFeatures = new ObservableField<>();
    public ObservableField<String> housingTitle = new ObservableField<>();
    public ObservableField<String> illustrate = new ObservableField<>();

    public ObservableField<String> Payment = new ObservableField<>();
    public ObservableField<String> rent = new ObservableField<>();


    public BindingCommand floorSel = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopWheelBase popWheelBase = new PopWheelBase(mActivity, 2, new PopWheelBase.Click() {
                @Override
                public void click(String s) {
                    floor.set(s);
                }
            });
            PopUtils.showPop(mActivity,popWheelBase,false);

            List<String> list1 = new ArrayList<>();
            for (int i = 1; i <49 ; i++) {
                list1.add("第"+i+"层");
            }

            List<String> list2 = new ArrayList<>();
            for (int i = 1; i <49 ; i++) {
                list2.add("共"+i+"层");
            }


            popWheelBase.setList1(list1);
            popWheelBase.setList2(list2);

        }
    });

    public BindingCommand liftSel = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopWheelBase popWheelBase = new PopWheelBase(mActivity, 1, s -> lift.set(s));
            PopUtils.showPop(mActivity,popWheelBase,false);

            List<String> list1 = new ArrayList<>();
            list1.add("有");
            list1.add("无");


            popWheelBase.setList1(list1);

        }
    });

    public BindingCommand renovationSel = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopWheelBase popWheelBase = new PopWheelBase(mActivity, 1, s -> renovation.set(s));
            PopUtils.showPop(mActivity,popWheelBase,false);

            List<String> list1 = new ArrayList<>();
            list1.add("豪华装修");
            list1.add("精装修");
            list1.add("简单装修");
            list1.add("毛坯");


            popWheelBase.setList1(list1);

        }
    });


    public BindingCommand PaymentSel = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopWheelBase popWheelBase = new PopWheelBase(mActivity, 1, s -> Payment.set(s));
            PopUtils.showPop(mActivity,popWheelBase,false);

            List<String> list1 = new ArrayList<>();
            list1.add("现金");
            list1.add("微信");
            list1.add("支付宝");
            list1.add("其他");


            popWheelBase.setList1(list1);

        }
    });

    public ObservableList<PopSelVM> ptList = new ObservableArrayList<>();
    public ItemBinding<PopSelVM> ptItem = ItemBinding.of(BR.viewModel, R.layout.item_quarter_sel);

    public ObservableList<PopSelVM> tsList = new ObservableArrayList<>();
    public ItemBinding<PopSelVM> tsItem = ItemBinding.of(BR.viewModel, R.layout.item_quarter_sel);


    public BindingCommand ptAdd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopCustomize popCustomize = new PopCustomize(mActivity,  s -> {
                ptList.clear();
                if (s.getName().contains(",")){
                    facilities.set(s.getName().substring(0,s.getName().length()-1));
                    String[] strings = s.getName().split(",");
                    for (String s1 : strings) {
                        PopSelVM popSelVM = new PopSelVM(AddMoreVM.this,s1);
                        ptList.add(popSelVM);
                    }
                }else {
                    facilities.set(s.getName());
                    PopSelVM popSelVM = new PopSelVM(AddMoreVM.this,s.getName());
                    ptList.add(popSelVM);
                }


            });
            PopUtils.showPop(mActivity,popCustomize,false);
            popCustomize.setTitle("房源配套");

            List<String> list = Arrays.asList(mActivity.getResources().getStringArray(R.array.ptList));
            List<PopBean> popBeanList = new ArrayList<>();
            for (String s : list) {
                PopBean popBean = new PopBean();
                popBean.setName(s);
                popBeanList.add(popBean);
            }

            popCustomize.setData(popBeanList);
        }
    });

    public BindingCommand tsAdd = new BindingCommand(() -> {
        PopCustomize popCustomize = new PopCustomize(mActivity,  s -> {
            tsList.clear();
            if (s.getName().contains(",")){
                RoomFeatures.set(s.getName().substring(0,s.getName().length()-1));
                String[] strings = s.getName().split(",");
                for (String s1 : strings) {
                    PopSelVM popSelVM = new PopSelVM(AddMoreVM.this,s1);
                    tsList.add(popSelVM);
                }
            }else {
                RoomFeatures.set(s.getName());
                PopSelVM popSelVM = new PopSelVM(AddMoreVM.this,s.getName());
                tsList.add(popSelVM);
            }

        });
        PopUtils.showPop(mActivity,popCustomize,false);
        popCustomize.setTitle("房间特色");

        List<String> list = Arrays.asList(mActivity.getResources().getStringArray(R.array.tsList));
        List<PopBean> popBeanList = new ArrayList<>();
        for (String s : list) {
            PopBean popBean = new PopBean();
            popBean.setName(s);
            popBeanList.add(popBean);
        }

        popCustomize.setData(popBeanList);
    });

    public BindingCommand saveInfo = new BindingCommand(() -> {
        entity.get().setFloor(floor.get());
        entity.get().setLift(lift.get());
        entity.get().setRenovation(renovation.get());
        entity.get().setPayment(Payment.get());
        entity.get().setRent(rent.get());
        entity.get().setToward(toward.get());
        entity.get().setFacilities(facilities.get());
        entity.get().setRoomFeatures(RoomFeatures.get());
        entity.get().setHousingTitle(housingTitle.get());
        entity.get().setIllustrate(illustrate.get());

        Intent intent = new Intent();
        intent.putExtra("data",entity.get());
        mActivity.setResult(10087,intent);
        finish();

    });


}
